#include <QApplication>

#include <UIKit/UIApplicationDelegate.h>
#include <UIKit/UIApplication.h>
#include <UIKit/UIDialogView.h>
#include <UIKit/UIButton.h>
#include <UIKit/UIWindow.h>
#include <UIKit/UIEvent.h>

#include "MokoTouch.h"

typedef int (*AppMainFunction) (int argc, char **argv);

MokoTouch::MokoTouch (QWidget *parent)
    : QGraphicsView(parent)
{
    // Setup GraphicView
    setAttribute(Qt::WA_OpaquePaintEvent);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setStyleSheet("border: none;");
    setBackgroundBrush(Qt::black);
    setMouseTracking(true);
    centerOn(0, 0);

    m_dialogView = NULL;

    // Handle Device Buttons
    UIDevice *device = UIDevice::currentDevice();
    connect(device, SIGNAL(auxButtonPressed(UIEvent *)),
            this, SLOT(auxButtonPressed(UIEvent *)));
    connect(device, SIGNAL(powerButtonPressed(UIEvent *)),
            this, SLOT(powerButtonPressed(UIEvent *)));
    connect(device, SIGNAL(orientationChanged(UIDeviceOrientation)),
            this, SLOT(orientationChanged(UIDeviceOrientation)));

    // Switch Application
    connect(UIApplication::sharedApplication(), 
            SIGNAL(keyWindowChanged(UIWindow *)),
            this, SLOT(keyWindowChanged(UIWindow *)));

    device->beginGeneratingDeviceOrientationNotifications();
}

MokoTouch::~MokoTouch() {
}

void MokoTouch::loadApp (const QString& appName, const QStringList& args) {
    UIApplication::sharedApplication()->loadByName(appName, args);
}

void MokoTouch::killApp (void) {
    UIApplication::sharedApplication()->quit();
    QCoreApplication::processEvents();
}

void MokoTouch::keyWindowChanged (UIWindow *window) {
    window->setSceneRect(0, 0, 480, 640);
    setBackgroundBrush(Qt::black);
    setScene(window);
}

void MokoTouch::orientationChanged (UIDeviceOrientation orientation) {
    if (m_dialogView == NULL)
        return;

    if (UIDeviceOrientationIsLandscape(orientation)) {
        m_dialogView->setSize(560, 400);
        m_dialogView->setPos(40, 40);

        foreach (QGraphicsItem *item, m_dialogView->childItems()) {
            if (UIView *view = dynamic_cast<UIView *>(item))
                view->setSize(520, view->height());
        }
    } else {
        m_dialogView->setSize(400, 560);
        m_dialogView->setPos(40, 40);

        foreach (QGraphicsItem *item, m_dialogView->childItems()) {
            if (UIView *view = dynamic_cast<UIView *>(item))
                view->setSize(360, view->height());
        }
    }
}

void MokoTouch::auxButtonPressed (UIEvent *event) {
    if (event->isAccepted())
        return;

    if (m_dialogView != NULL) {
        closeDialogView();
    }

    // TODO:
    UIApplication::sharedApplication()->loadByName("Homescreen");

    event->accept();
}

void MokoTouch::powerButtonPressed (UIEvent *event) {
    if (event->isAccepted())
        return;

    if (m_dialogView != NULL) {
        closeDialogView();
        event->accept();
        return;
    }

    UIDevice *device = UIDevice::currentDevice();

    m_dialogView = new UIDialogView;
    connect(m_dialogView, SIGNAL(closeTouched()), this, SLOT(closeDialogView()));
    m_dialogView->setTitle("Halt/Reboot System");
    m_dialogView->setSize(560, 400);
    m_dialogView->setPos(40, 40);
    m_dialogView->setZValue(2.0f);

    QLinearGradient gradient(0.0f, 0.0f, 0.0f, 96.0f);
    gradient.setColorAt(0.0f, QColor(0x39, 0x50, 0x79));
    gradient.setColorAt(1.0f, QColor(0x39, 0x50, 0x79));

    UIButton *button = new UIButton(UIButtonTypeRoundedRect, m_dialogView);
    connect(button, SIGNAL(doubleTap(UITouch *)), device, SLOT(systemReboot()));
    button->setBackgroundColor(gradient);
    button->setTitleColor(Qt::white);
    button->setTitleShadowColor(Qt::lightGray);
    button->setBorderColor(QColor(0xa6, 0xac, 0xb3));
    button->setTitleShadowOffset(0.2f);
    button->setTitle(tr("Reboot System!"));
    button->setSize(520.0f, 100.0f);
    button->setPos(20.0f, 125.0f);

    button = new UIButton(UIButtonTypeRoundedRect, m_dialogView);
    connect(button, SIGNAL(doubleTap(UITouch *)), device, SLOT(systemShutdown()));
    button->setBackgroundColor(gradient);
    button->setTitleColor(Qt::white);
    button->setTitleShadowColor(Qt::lightGray);
    button->setBorderColor(QColor(0xa6, 0xac, 0xb3));
    button->setTitleShadowOffset(0.2f);
    button->setTitle("Halt System!");
    button->setSize(520.0f, 100.0f);
    button->setPos(20.0f, 250.0f);

    orientationChanged(device->orientation());
    UIApplication::sharedApplication()->keyWindow()->addItem(m_dialogView);

    event->accept();
}

void MokoTouch::closeDialogView (void) {
    if (m_dialogView != NULL) {
        m_dialogView->scene()->removeItem(m_dialogView);
        m_dialogView = NULL;
    }
}


